package closure;

/**
 * @author: fqtang
 * @date: 2024/12/21/6:48
 * @description: 闭包测试
 */
public class ClosureTest {

	public static void test(MyLambda lambda) {
		System.out.println(lambda.op(2));
	}

	public static void main(String[] args) {
		/*
		 * 函数对象(y -> x+y 与它外部的变量x形成了闭包)
		 *
		 * effectively final
		 */
		int x = 2;
		test(y -> x + y);
		Student student = new Student(10);
		MyLambda myLambda = s-> student.age+x;
		test(myLambda);
		student.age =100;
		test(myLambda);

		ClosureTest closureTest = new ClosureTest();
		closureTest.test2();
	}

	int a = 1;
	static int c = 1;

	public void test2() {
		int b = 1;
		test(y -> a + c);
	}

	static class Student {
        int age;

		public Student(int age) {
			this.age = age;
		}
	}
}
